// subcontract/Classify/classifyDetails/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: ['找产品', '找厂家'],
    activeIndex: 0,//选择的下标
    scrollIndex: 0,//选择的第几个分类
    screenIndex: 0,//选择的第几个筛选
    isRefreshing: false,//开启下拉刷新提示
    page: 1,//页码
    pagingState: false,//分页状态
    sid: '',//分类id
    sort_by_price: '',//价格
    sort_by_sales: '',//销量
    sort_by_create: '',//新品
    goodsList: [],//商品列表
    children: [],//品牌
    scrollleft: 0
  },
  /** */
  onChange(event) {
    console.log(event);
    let index = event.detail.index
    this.setData({
      activeIndex: index,
      page: 1,//页码
      pagingState: false,//分页状态
      goodsList: [],//商品列表
      screenIndex: 0
    })
    if (index == 0) {
      this.getGoodsList(this.data.children[this.data.scrollIndex].id)
    } else {
      this.getShopList(this.data.children[this.data.scrollIndex].id)
    }
  },
  /**找厂家 */
  async getShopList(id) {
    let object = {
      page_no: this.data.page,
      third_cate_id: id,
      type: this.data.screenIndex
    }
    if (!this.data.pagingState) {
      const { code, data } = await getApp().api.getShopList(object)
      if (code == 1) {
        this.setData({
          goodsList: this.data.goodsList.concat(data.list).map(item => {
            item.goods_list = item.goods_list.slice(0, 4)
            return item
          })
        })
        if (data.more != 1) {
          this.setData({
            pagingState: true
          })
          return
        }
        this.setData({
          page: (this.data.page * 1) + 1
        })
      }
    }
  },
  switchc(event) {
    let index = event.currentTarget.dataset.index
    this.setData({
      page: 1,//页码
      pagingState: false,//分页状态
      goodsList: [],//商品列表
      screenIndex: index,
    })
    this.getShopList(this.data.children[this.data.scrollIndex].id)
  },
  /**切换 */
  switch(event) {
    let type = event.currentTarget.dataset.type
    let index = event.currentTarget.dataset.index
    this.setData({
      // scrollleft: index > this.data.activeIndex ? this.data.scrollleft + 50 : this.data.scrollleft - 50,
      page: 1,
      pagingState: false,
      goodsList: [],
    })
    if (type == '品牌') {
      this.setData({
        screenIndex: 0,
        sort_by_create: '',
        sort_by_price: '',
        sort_by_sales: ''
      })
    }
    this.setData(type == '品牌' ? { scrollIndex: index } : { screenIndex: index })
    if (this.data.activeIndex == 0) {
      this.getGoodsList(this.data.children[this.data.scrollIndex].id)
    } else {
      this.getShopList(this.data.children[this.data.scrollIndex].id)
    }
  },
  /**
 * 跳转页面
 */
  navigateTo(event) {
    let url = event.currentTarget.dataset.url
    wx.navigateTo({
      url,
    })
  },
  /**二级分类列表   筛选选择的哪一个*/
  async getLevelOneList(id, rid, sid) {
    const { code, data } = await getApp().api.getListByLevelOne(id)
    if (code == 1) {
      let zindex = data.findIndex(item => item.id == rid)
      let scrollIndex = data[zindex].children.findIndex(item => item.id == sid)
      this.setData({
        scrollIndex,
        children: data[zindex].children
      })
    }
  },
  /**商品列表 */
  async getGoodsList(id) {
    function toggleSort(currentSort) {
      return currentSort == 'asc' ? 'desc' : 'asc';
    }
    let index = this.data.screenIndex
    if (index == 0 || index == 1 || index == 2 || index == 3) {
      if (index == 0) {
        this.setData({
          sort_by_create: '',
          sort_by_price: '',
          sort_by_sales: ''
        })
      } else if (index == 1) {
        this.setData({
          sort_by_create: toggleSort(this.data.sort_by_create),
          sort_by_price: '',
          sort_by_sales: ''
        })
      } else if (index == 2) {
        this.setData({
          sort_by_create: '',
          sort_by_price: '',
          sort_by_sales: toggleSort(this.data.sort_by_sales),
        })
      } else if (index == 3) {
        this.setData({
          sort_by_create: '',
          sort_by_price: toggleSort(this.data.sort_by_price),
          sort_by_sales: ''
        })
      }
    }
    let object = {
      platform_cate_id: id,
      sort_by_price: this.data.sort_by_price, // 初始化价格排序为空
      sort_by_sales: this.data.sort_by_sales, // 初始化销量排序为空
      sort_by_create: this.data.sort_by_create, // 新品
      page_no: this.data.page
    };
    if (!this.data.pagingState) {
      const { code, data } = await getApp().api.getGoodsList(object)
      if (code == 1) {
        this.setData({
          goodsList: this.data.goodsList.concat(data.lists)
        })
        if (data.more != 1) {
          this.setData({
            pagingState: true
          })
          return
        }
        this.setData({
          page: (this.data.page * 1) + 1
        })
        console.log(this.data.page);
      }
    }
  },
  /**
 * 生命周期函数--监听页面加载
 */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: options.title,
    })
    this.setData({
      yid: options.id,
      rid: options.rid,
      sid: options.sid
    })
    this.getLevelOneList(options.id, options.rid, options.sid)
    this.getGoodsList(options.sid)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    // 在这里处理下拉刷新的逻辑

    // 刷新完成后，调用 stopPullDownRefresh 停止下拉刷新动画
    // wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.activeIndex == 0) {
      this.getGoodsList(this.data.children[this.data.scrollIndex].id)
    } else {
      this.getShopList(this.data.children[this.data.scrollIndex].id)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})